/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.client.stores;

import java.util.ArrayList;
import java.util.Collection;

import org.jb.sharedbills.client.gin.EventId;
import org.jb.sharedbills.shared.dto.EventInfo;
import org.jb.sharedbills.shared.model.Event;
import org.jb.sharedbills.shared.model.logic.BillCalculator;
import org.jb.sharedbills.shared.svc.EventServiceAsync;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * 
 * Mutualise les accès au service de lecture des événements.
 * 
 * @author jeremie
 *
 */
@Singleton
public class EventStore implements EventLoader {

	/**
	 * 
	 * @author jeremie
	 *
	 */
	public enum State {
		READY,
		LOADING,
		LOADED,
		ERROR
	}
	
	/**
	 * 
	 * @param eventService
	 */
	@Inject
	public EventStore( EventServiceAsync eventService, EventSaver eventSaver, @EventId String eventId ) {
		this.eventService = eventService;
		this.eventSaver = eventSaver;
		this.eventId = eventId;
	}
	
	/**
	 * 
	 * @return
	 */
	public Event getEvent() {
		return eventInfo.getEvent();
	}
	
	/**
	 * @param failure the failure to set
	 */
	public void setFailure(Throwable failure) {
		this.failure = failure;
	}

	/**
	 * @return the failure
	 */
	public Throwable getFailure() {
		return failure;
	}

	/**
	 * 
	 */
	private void load() {
		state = State.LOADING;
				
		if (eventId == null)
			return;
		
		eventService.getEventInfo( eventId, new AsyncCallback<EventInfo>() {

			/*
			 * (non-Javadoc)
			 * @see com.google.gwt.user.client.rpc.AsyncCallback#onFailure(java.lang.Throwable)
			 */
			@Override
			public void onFailure(Throwable caught) {
				state = State.ERROR;
				failure = caught;
				
				for( AsyncCallback<Event> callback : waitingEventCallback ) {
					callback.onFailure(caught);
				}
			}

			/*
			 * 
			 */
			@Override
			public void onSuccess(EventInfo result) {
				state = State.LOADED;
				eventInfo = result;
				
				if (eventInfo.getEvent() != null) {
					if (eventInfo.isWriteable()) {
						new BillCalculator(eventInfo.getEvent());
					}
					eventSaver.register(eventInfo.getEvent(), eventInfo.isWriteable());
				}
				
				for( AsyncCallback<Event> callback : waitingEventCallback ) {
					callback.onSuccess(eventInfo.getEvent());
				}	
			}
		});
	}
	
	/**
	 * 
	 */
	@Override
	public void load(AsyncCallback<Event> callback) {
		
		switch( state ) {
		case READY: {
			waitingEventCallback.add( callback );
			load();
			break;
		}
		case LOADING: {
			waitingEventCallback.add( callback );
			break;
		}
		case LOADED: {
			callback.onSuccess( getEvent() );
		}
		case ERROR: {
			callback.onFailure( getFailure() );
		}
		}	
	}
	
	
	private EventInfo eventInfo = null;
	private Throwable failure = null;
	private State state = State.READY;
	private final EventServiceAsync eventService;
	private final String eventId;
	private Collection< AsyncCallback<Event> > waitingEventCallback = new ArrayList< AsyncCallback<Event> >();
	final private EventSaver eventSaver;


	
}
